package com.jrx.anytxn.param.service.couponReport.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.dto.couponReport.TlCouponSummaryRes;
import com.jrx.anytxn.param.entity.TlCouponSummary;
import com.jrx.anytxn.param.mapper.base.TlCouponSummaryMapper;
import com.jrx.anytxn.param.mapper.ext.ExtTlCouponSummaryMapper;
import com.jrx.anytxn.param.service.couponReport.ITlCouponSummaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TlCouponSummaryServiceImpl implements ITlCouponSummaryService {

    private static final Logger logger = LoggerFactory.getLogger(TlCouponSummaryServiceImpl.class);

    @Resource
    private ExtTlCouponSummaryMapper extTlCouponSummaryMapper;
    @Resource
    private TlCouponSummaryMapper tlCouponSummaryMapper;

    @Override
    public TxnPage<TlCouponSummaryRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<TlCouponSummaryRes> page = PageHelper.startPage(pageNum, pageSize);
        List<TlCouponSummary> tableList = extTlCouponSummaryMapper.selectByTenantId(tenantId);
        if (tableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<TlCouponSummaryRes> res = BeanMapping.copyList(tableList, TlCouponSummaryRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public void addCouponSummary(TlCouponSummary tlCouponSummary) {
        tlCouponSummaryMapper.insertSelective(tlCouponSummary);
    }

    @Override
    public TlCouponSummary findById(Long id,String tenantId) {
        return extTlCouponSummaryMapper.selectByIdAndTenantId(id,tenantId);
    }
}
